PLUGIN.Name = "Achievements"
PLUGIN.DefaultOn = false
PLUGIN.Description = "Prints progress about GMod's stupid achievements"
PLUGIN.Trigger = false

PLUGIN.DidTryLoadParams = false
PLUGIN.DidTryLoad       = false

function PLUGIN:LoadParameters()
	print( achievements )
	if not achievements then
		if not self.DidTryLoadParams then
			timer.Simple( 0, self.LoadParameters, self )
			self.DidTryLoadParams = true
			
		end
		return
	
	end
	
	local orden = {}
	local count = achievements.Count()
	for i=1,count do
		table.insert( orden,
			{ i,
			tostring( achievements.GetName( i ) or ("<unknown:" .. i .. ">") )
			}
		)
		
	end
	table.sort(orden, function(a,b) return a[2] < b[2] end)

	for i=1,count do
		self:AddParameter("ach".. orden[i][1] , { Type = "checkbox" , Text = orden[i][2], Defaults = "0" , OnChange = function() self:_Constitute() end } )
		
	end
	
	self:AddParameter("x_relpos",    { Type = "slider" , Text = "Grid X position", Defaults = "0", Min = "0", Max = "16", Decimals = "0" } )
	self:AddParameter("y_relpos",    { Type = "slider" , Text = "Grid Y position", Defaults = "0", Min = "0", Max = "16", Decimals = "0" } )
	self:AddParameter("padding",      { Type = "slider"   , Text = "Padding", Defaults = "4", Min = "0", Max = "8", Decimals = "0" } )

	
end

function PLUGIN:_Constitute()
	self.AT = {}
	
	local count = achievements.Count()
	for i=1,count do
		if self:GetNumber("ach".. i) > 0 then
			table.insert( self.AT,
				{ i,
				tostring(achievements.GetName( i ) or ""), 
				tostring(achievements.GetDesc( i ) or ""), 
				achievements.GetGoal( i ) or 0 }
			)
			
		end
		
	end
	
	table.sort(self.AT, function(a,b) return a[2] < b[2] end)
	
end

function PLUGIN:Load()
	if not achievements then
		if not self.DidTryLoad then
			timer.Simple( 0, self.Load, self )
			self.DidTryLoad = true
			
		end
		return
	
	end
	
	self.CO_Main   =  Color( 0, 0, 0, 128 )
	self.CO_Text   =  Color( 255, 255, 255, 255 )
	self.CO_Achieving   =  Color( 255, 255, 255, 64 )
	self.BaseWidth = 280

	self:_Constitute()
	
end

function PLUGIN:Unload()
	
end

function PLUGIN.HOOK:HUDPaint()
	if not self.AT or #self.AT == 0 then return end

	local padding = self:GetNumber("padding")
	local X_Ref = 0
	local Y_Ref = 0
	
	local EXTRA = 5
	local heightnorm = #self.AT * 17 + (#self.AT - 1) * 3 + EXTRA
	
	X_Ref = padding + self:GetNumber("x_relpos") * ((ScrW() - 2 * padding - self.BaseWidth) / 16 )
	Y_Ref = padding + self:GetNumber("y_relpos") * ((ScrH() - 2 * padding - heightnorm) / 16 )
	
	surface.SetDrawColor( self.CO_Main.r , self.CO_Main.g, self.CO_Main.b, self.CO_Main.a )
	surface.DrawRect( X_Ref, Y_Ref, self.BaseWidth, heightnorm )
	
	surface.SetDrawColor( self.CO_Achieving.r , self.CO_Achieving.g, self.CO_Achieving.b, self.CO_Achieving.a )
	for i=1,#self.AT do
		draw.SimpleText( self.AT[i][2], "ScoreboardText", X_Ref + 5, Y_Ref + 20 * (i-1), self.CO_Text, TEXT_ALIGN_LEFT )
		draw.SimpleText( self.AT[i][3], "DefaultSmall", X_Ref + 5 + 5, Y_Ref + 12 + 20 * (i-1), self.CO_Text, TEXT_ALIGN_LEFT )
		
		if self.AT[i][4] > 1 then
			local count = achievements.GetCount( self.AT[i][1] )
			local goal = self.AT[i][4]
			draw.SimpleText( count .. "/" .. goal, "ScoreboardText", X_Ref + self.BaseWidth - 5, Y_Ref + 20 * (i-1), self.CO_Text, TEXT_ALIGN_RIGHT )
			surface.DrawRect( X_Ref + 2 + (self.BaseWidth - 4) / 2, Y_Ref + 2 + 20 * (i-1), math.Clamp(count / goal, 0, 1) * (self.BaseWidth - 4) / 2, 17 - 4 )
		else
			if achievements.IsAchieved( self.AT[i][1] ) then
				draw.SimpleText( "Done !", "ScoreboardText", X_Ref + self.BaseWidth - 5, Y_Ref + 20 * (i-1), self.CO_Text, TEXT_ALIGN_RIGHT )
			end
		end
	end
	
end
